package com.cm.sys.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cm.sys.framework.utils.JwtUtils;
import com.cm.sys.system.entity.User;
import com.cm.sys.system.entity.UserRole;
import com.cm.sys.system.service.UserRoleService;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.application.RequestParameter;
import com.cm.sys.framework.application.ResponseResult;
import com.cm.sys.framework.execption.BusinessException;
import com.cm.sys.framework.utils.JsonUtil;
import com.cm.sys.system.entity.Role;
import com.cm.sys.system.service.RoleAuthService;
import com.cm.sys.system.service.RoleService;

/***
 * 角色管理
 * 
 * @author yxq18
 *
 */
@Controller
@RequestMapping(value = "/user")
public class RoleUserController {

    private static final Logger logger = LoggerFactory.getLogger(RoleUserController.class);

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleAuthService roleAuthService;

    @Autowired
    private UserRoleService userRoleService;

    /***
     * 查询角色
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public ResponseResult getUser(RequestParameter rp) {

        JSONObject args = rp.getparameters();

        ResponseResult rr = new ResponseResult();
        List<Role> list = roleService.selectRoles(args);

        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("lists", list);
        rr.setMap(map);
        return rr;

    }

    /***
     * 查询角色
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/role/{id}", method = RequestMethod.GET)
    public ResponseResult getUser(RequestParameter rp, @PathVariable("id") String id) {

        ResponseResult rr = new ResponseResult();

        if (StringUtils.isEmpty(id)) {
            throw new BusinessException(ApiResponseCode.CODE_LOGIN.getCode(), "路由错误缺少id");
        }
        Role userRole = roleService.selectRole(Integer.parseInt(id));

        rr.setObject(userRole);

        return rr;

    }

    /***
     * 删除角色
     *
     * @return
     */
    @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE)
    public ResponseResult deleteUser(RequestParameter rp, @PathVariable("id") String id) {
        ResponseResult rr = new ResponseResult();

        if (StringUtils.isEmpty(id)) {
            throw new BusinessException(ApiResponseCode.CODE_LOGIN.getCode(), "路由错误缺少id");
        }
        //1.1 查询当前删除角色是否有关联的用户
        List<UserRole> users = userRoleService.selectByRoleId(Integer.parseInt(id));
//      //1.2 判断此用户组列表是否为空
//          if(users.size()>0){
//              //1.22 不为空，则表示内含多个员工，不可被删除
//              throw new BusinessException(ApiResponseCode.NO_DATA.getCode(), "当前用户组存在员工，不允许删除");
//          }

        //2. 执行删除操作
        int count = roleService.delRole(rp.getparameters(), Integer.parseInt(id));
        if (count != 1) {
            rr.setCode(ApiResponseCode.NO_DEL_DATA.getCode());
            return rr;
        }
        //删除user_role表中的相关联数据
        Integer cnt = userRoleService.deleteByRoleId(Integer.parseInt(id));
        //删除role_auth表中的关联数据 根据此roleid
        int i = roleAuthService.delAuth(rp.getparameters(), Integer.parseInt(id));

        return rr;
    }

    /***
     * 更新角色
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/role/{id}", method = RequestMethod.PUT)
    public ResponseResult putUser(RequestParameter rp, @PathVariable("id") String id) {
        ResponseResult rr = new ResponseResult();

        if (StringUtils.isEmpty(id)) {
            throw new BusinessException(ApiResponseCode.CODE_LOGIN.getCode(), "路由错误缺少id");
        }
        Role userRole = roleService.updateRole(rp.getparameters(), Integer.parseInt(id));
        rr.setObject(userRole);
        return rr;

    }

    /***
     * 新增角色
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public ResponseResult postUser(RequestParameter rp) {
        ResponseResult rr = new ResponseResult();
        JSONObject args = rp.getparameters();
        String roleName = JsonUtil.getString(args, "roleName");
        String roleDescrpt = JsonUtil.getString(args, "roleDescrpt");

        JSONObject json = new JSONObject();
        json.put("roleName", roleName);
        json.put("roleDescrpt", roleDescrpt);

        Role userRole = roleService.saveRole(json);
        rr.setObject(userRole);
        return rr;

    }

}
